import { EntitySchema, Entity } from "typeorm";
import { createTableName } from "../../extend/createTableName"

export const tableNameFactory = createTableName('trade_history')
export interface KLineDTO {
    id: number;
    symbol: string;
    buy: number;
    sell: number;
    volume: number;
    time: Date;
    open: number;
    low: number;
    high: number;
    close: number;
    ask_depth_count?: number
    bid_depth_count?: number
}
export const entitysMap: Record<string, EntitySchema<KLineDTO>> = {}

export function createEntitySchema(year?: string | number) {
    const tableName = tableNameFactory.getTableName(year)
    if (entitysMap[year]) {
        return entitysMap[year];
    }

    entitysMap[year] = new EntitySchema<KLineDTO>({
        name: `trade_history_${year}`,
        tableName: tableName,
        columns: {
            id: {
                type: 'int',
                primary: true,
                generated: true,
                unsigned: true
            },
            symbol: {
                type: 'varchar',
                length: 127,
                default: '',
            },
            open: {
                type: 'float',
                default: 0.0,
                unsigned: true,
            },
            close: {
                type: 'float',
                default: 0.0,
                unsigned: true
            },
            low: {
                type: 'float',
                default: 0.0,
                unsigned: true,
            },
            high: {
                type: 'float',
                default: 0.0,
                unsigned: true,
            },
            buy: {
                type: 'float',
                default: 0.0,
                unsigned: true
            },
            sell: {
                type: 'float',
                default: 0.0,
                unsigned: true
            },
            volume: {
                type: 'float',
                default: 0.0,
                unsigned: true
            },
            time: {
                type: Date
            },
            bid_depth_count: {
                type: 'int',
                default: 0,
                unsigned: true
            },
            ask_depth_count: {
                type: 'int',
                default: 0,
                unsigned: true
            },
        },
        indices: [
            {
                name: 'IDX_symbol',
                columns: ['symbol']
            }
        ]
    });
    return entitysMap[year];
}


for (let year = 2017; year <= new Date().getFullYear() + 1; year++) {
    createEntitySchema(year)
}

